package com.ctshk.rpc.workflow.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ctshk.common.constant.SystemConstants;
import com.ctshk.common.enums.IsDeletedCode;
import com.ctshk.common.enums.SysApprovalType;
import com.ctshk.common.enums.SystemBusinessType;
import com.ctshk.common.enums.SystemError;
import com.ctshk.common.exception.BusinessException;
import com.ctshk.common.model.PageResponse;
import com.ctshk.common.req.DepartmentData;
import com.ctshk.common.utils.EntityUtil;
import com.ctshk.common.utils.SnowflakeIdWorker;
import com.ctshk.rpc.system.dto.UserDTO;
import com.ctshk.rpc.system.service.ISysDepartmentService;
import com.ctshk.rpc.system.service.ISysUserService;
import com.ctshk.rpc.workflow.dto.*;
import com.ctshk.rpc.workflow.entity.*;
import com.ctshk.rpc.workflow.mapper.*;
import com.ctshk.rpc.workflow.req.ApprovalSettingQueryReq;
import com.ctshk.rpc.workflow.req.ApprovalSettingReq;
import com.ctshk.rpc.workflow.req.ApprovalSettingStepReq;
import com.ctshk.rpc.workflow.service.ISysApprovalSettingService;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * <p>
 * 审批配置表 服务实现类
 * </p>
 *
 * @author 谢诗宏
 * @since 2021-01-25
 */
@DubboService
public class SysApprovalSettingServiceImpl extends ServiceImpl<SysApprovalSettingMapper, SysApprovalSetting> implements ISysApprovalSettingService {

    @Autowired
    private SysApprovalSettingMapper settingMapper;
    @Autowired
    private SysApprovalSettingMemberMapper settingMemberMapper;
    @Autowired
    private SysApprovalSettingConditionMapper settingConditionMapper;
    @Autowired
    private SysApprovalSettingSponsorMapper settingSponsorMapper;
    @Autowired
    private SysApprovalSettingStepMapper settingStepMapper;

    @DubboReference
    private ISysUserService sysUserService;
    @DubboReference
    private ISysDepartmentService sysDepartmentService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public long add(ApprovalSettingReq settingReq, long userId) {
        SysApprovalSetting setting = new SysApprovalSetting();
        Long settingId = SnowflakeIdWorker.nextId();
        setting.setId(settingId);
        setting.setBusinessType(settingReq.getBusinessType());
        setting.setBusinessTypeName(SystemBusinessType.parseValue(settingReq.getBusinessType()));
        setting.setApprovalType(settingReq.getApprovalType());
        setting.setApprovalTypeName(SysApprovalType.parseValue(settingReq.getApprovalType()));
        setting.setSysCode(SystemConstants.SYSTEM_CODE + RandomUtil.randomNumbers(8));
        setting.setName(settingReq.getName());
        setting.setCreateId(userId);
        int insert = settingMapper.insert(setting);
        if (insert > 0) {
            settingReq.setId(settingId);
            addOthers(settingReq, userId);
        }
        return settingId;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public long del(long id, long userId) {
        /**
         * 删除审批模板主表
         */
        SysApprovalSetting setting = new SysApprovalSetting();
        setting.setId(id);
        setting.setModifiedId(userId);
        setting.setIsDeleted(1);
        int i = settingMapper.updateById(setting);
        if(i > 0){
            delOthers(id, userId);
        }
        return id;
    }

    @Override
    public long update(ApprovalSettingReq settingReq, long userId) {
        /**
         * 更新主表
         */
        SysApprovalSetting setting = new SysApprovalSetting();
        Long settingId = settingReq.getId();
        if (settingId == null) {
            throw new BusinessException(SystemError.SYS_412);
        }
        setting.setId(settingId);
        setting.setBusinessType(settingReq.getBusinessType());
        setting.setBusinessTypeName(SystemBusinessType.parseValue(settingReq.getBusinessType()));
        setting.setApprovalType(settingReq.getApprovalType());
        setting.setApprovalTypeName(SysApprovalType.parseValue(settingReq.getApprovalType()));
        setting.setName(settingReq.getName());
        setting.setModifiedId(userId);
        int i = settingMapper.updateById(setting);
        if (i > 0) {
            delOthers(settingId, userId);
            settingReq.setId(settingId);
            addOthers(settingReq, userId);
        }
        return settingId;
    }

    @Override
    public ApprovalSettingInfoDTO query(long id) {
        SysApprovalSetting setting = settingMapper.selectById(id);
        if (setting == null || setting.getIsDeleted() == 1) {
            throw new BusinessException(SystemError.SYS_411);
        }
        ApprovalSettingInfoDTO settingInfoDTO = new ApprovalSettingInfoDTO();
        settingInfoDTO.setId(setting.getId());
        settingInfoDTO.setBusinessType(setting.getBusinessType());
        settingInfoDTO.setBusinessTypeName(setting.getBusinessTypeName());
        settingInfoDTO.setApprovalType(setting.getApprovalType());
        settingInfoDTO.setApprovalTypeName(setting.getApprovalTypeName());
        settingInfoDTO.setName(setting.getName());
        settingInfoDTO.setSysCode(setting.getSysCode());

        /**
         * 获取发起人范围 start
         */
        List<DepartmentData> employees = new ArrayList<>();
        List<DepartmentData> depts = new ArrayList<>();
        List<SysApprovalSettingSponsor> settingSponsors = settingSponsorMapper.selectList(Wrappers.<SysApprovalSettingSponsor>lambdaQuery()
                .eq(SysApprovalSettingSponsor::getSettingId, setting.getId())
                .eq(SysApprovalSettingSponsor::getIsDeleted, 0));
        Optional.ofNullable(settingSponsors).orElse(new ArrayList<>()).forEach(settingSponsor -> {
            DepartmentData departmentData = new DepartmentData();
            departmentData.setId(settingSponsor.getSponsorObjectId());
            departmentData.setTitle(settingSponsor.getSponsorObjectName());
            if (settingSponsor.getSponsorType() == 1) {
                // 发起人类型是部门
                depts.add(departmentData);
            } else if (settingSponsor.getSponsorType() == 2) {
                // 发起人类型是员工
                employees.add(departmentData);
            }
        });
        /**
         * 获取发起人范围 start
         */

        /**
         * 获取步骤数组 start
         */
        List<ApprovalSettingStepDTO> steps = new ArrayList<>();
        List<SysApprovalSettingStep> settingSteps = settingStepMapper.selectList(Wrappers.<SysApprovalSettingStep>lambdaQuery()
                .eq(SysApprovalSettingStep::getSettingId, setting.getId())
                .eq(SysApprovalSettingStep::getIsDeleted, 0));
        Optional.ofNullable(settingSteps).orElse(new ArrayList<>()).forEach(settingStep -> {
            ApprovalSettingStepDTO settingStepDTO = new ApprovalSettingStepDTO();
            settingStepDTO.setId(settingStep.getId());
            settingStepDTO.setStep(settingStep.getStep());
            settingStepDTO.setStepName(settingStep.getStepName());
            settingStepDTO.setIsDynamic(settingStep.getIsDynamic());
            // 获取当前审批步骤的审批条件
            SysApprovalSettingCondition settingCondition = settingConditionMapper.selectOne(Wrappers.<SysApprovalSettingCondition>lambdaQuery()
                    .eq(SysApprovalSettingCondition::getSettingId, setting.getId())
                    .eq(SysApprovalSettingCondition::getStepId, settingStep.getId())
                    .eq(SysApprovalSettingCondition::getIsDeleted, 0));
            settingStepDTO.setCondition(EntityUtil.copy(settingCondition, ApprovalSettingConditionDTO.class));

            Long conditionId = null;

            if (null != settingCondition) {
                conditionId = settingCondition.getId();
                // 根据当前步骤和条件获取副审批人(没有关联条件的则就是副审批人)
                List<SysApprovalSettingMember> slaveMember = settingMemberMapper.selectList(Wrappers.<SysApprovalSettingMember>lambdaQuery()
                        .eq(SysApprovalSettingMember::getSettingId, setting.getId())
                        .eq(SysApprovalSettingMember::getStepId, settingStep.getId())
                        .eq(SysApprovalSettingMember::getIsRelationCondition, 0)
                        .eq(SysApprovalSettingMember::getIsDeleted, 0));
                List<DepartmentData> employeesApprovered = new ArrayList<>();
                List<DepartmentData> deptsApprovered = new ArrayList<>();
                slaveMember.forEach(member -> {
                    DepartmentData departmentData = new DepartmentData();
                    departmentData.setId(member.getApproverId());
                    departmentData.setTitle(member.getApproverName());
                    if (member.getApproverType() == 1) {
                        // 发起人类型是部门
                        deptsApprovered.add(departmentData);
                    } else if (member.getApproverType() == 2) {
                        // 发起人类型是员工
                        employeesApprovered.add(departmentData);
                    }
                });
                ApprovalSettingMemberDTO memberDTO = new ApprovalSettingMemberDTO();
                memberDTO.setDepts(deptsApprovered);
                memberDTO.setEmployees(employeesApprovered);
                settingStepDTO.setSlaveMember(memberDTO);
            }

            // 根据当前步骤和条件获取主审批人
            List<SysApprovalSettingMember> masterMember = settingMemberMapper.selectList(Wrappers.<SysApprovalSettingMember>lambdaQuery()
                    .eq(SysApprovalSettingMember::getSettingId, setting.getId())
                    .eq(SysApprovalSettingMember::getStepId, settingStep.getId())
                    .eq(null != conditionId, SysApprovalSettingMember::getConditionId, conditionId)
                    .eq(SysApprovalSettingMember::getIsDeleted, 0));
            List<DepartmentData> employeesApprovered = new ArrayList<>();
            List<DepartmentData> deptsApprovered = new ArrayList<>();
            masterMember.forEach(member -> {
                DepartmentData departmentData = new DepartmentData();
                departmentData.setId(member.getApproverId());
                departmentData.setTitle(member.getApproverName());
                if (member.getApproverType() == 1) {
                    // 发起人类型是部门
                    deptsApprovered.add(departmentData);
                } else if (member.getApproverType() == 2) {
                    // 发起人类型是员工
                    employeesApprovered.add(departmentData);
                }
            });
            ApprovalSettingMemberDTO memberDTO = new ApprovalSettingMemberDTO();
            memberDTO.setDepts(deptsApprovered);
            memberDTO.setEmployees(employeesApprovered);
            settingStepDTO.setMasterMember(memberDTO);

            steps.add(settingStepDTO);
        });
        /**
         * 获取步骤数组 end
         */

        settingInfoDTO.setDepts(depts);
        settingInfoDTO.setEmployees(employees);
        settingInfoDTO.setSteps(steps);

        return settingInfoDTO;
    }

    @Override
    public PageResponse<ApprovalSettingListDTO> getList(ApprovalSettingQueryReq settingQueryReq) {
        Page<SysApprovalSetting> iPage = new Page<>(settingQueryReq.getPageNo(), settingQueryReq.getPageSize());
        QueryWrapper<SysApprovalSetting> queryWrapper = new QueryWrapper();
        queryWrapper.lambda()
                .eq(settingQueryReq.getBusinessTypeId() != 0, SysApprovalSetting::getBusinessType, settingQueryReq.getBusinessTypeId())
                .eq(settingQueryReq.getApprovalTypeId() != 0, SysApprovalSetting::getApprovalType, settingQueryReq.getApprovalTypeId())
                .eq(settingQueryReq.getCreateId() != 0, SysApprovalSetting::getCreateId, settingQueryReq.getCreateId())
                .like(StringUtils.isNotBlank(settingQueryReq.getName()), SysApprovalSetting::getName, settingQueryReq.getName())
                .eq(SysApprovalSetting::getIsDeleted, 0)
                .orderByDesc(SysApprovalSetting::getGmtCreate);
        IPage<SysApprovalSetting> settingPage = settingMapper.selectPage(iPage, queryWrapper);
        if (CollUtil.isEmpty(settingPage.getRecords())) {
            return PageResponse.empty(settingPage.getCurrent());
        }
        List<ApprovalSettingListDTO> dtos = new ArrayList<>();
        settingPage.getRecords().forEach(setting -> {
            ApprovalSettingListDTO dto = EntityUtil.copy(setting, ApprovalSettingListDTO.class);
            UserDTO userDTO = sysUserService.queryCacheUser(setting.getCreateId());
            dto.setDept(userDTO.getTitle());
            dto.setCreateName(userDTO.getFullName());
            dtos.add(dto);
        });

        return new PageResponse<>(dtos, iPage.getCurrent() == 1, iPage.getTotal() <= (iPage.getCurrent() * iPage.getSize()), iPage.getTotal(), iPage.getCurrent(), iPage.getSize());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public long copy(long id, long userId) {
        SysApprovalSetting setting = settingMapper.selectById(id);
        if (setting == null || setting.getIsDeleted() == 1) {
            throw new BusinessException(SystemError.SYS_411);
        }
        // 复制生成审批模板主表内容
        Long settingId = SnowflakeIdWorker.nextId();
        setting.setId(settingId);
        setting.setName(setting.getName() + " - 副本");
        setting.setSysCode(SystemConstants.SYSTEM_CODE + RandomUtil.randomNumbers(8));
        setting.setGmtCreate(LocalDateTime.now());
        setting.setGmtModified(null);
        settingMapper.insert(setting);

        /**
         * 获取发起人范围 start
         */
        List<SysApprovalSettingSponsor> settingSponsors = settingSponsorMapper.selectList(Wrappers.<SysApprovalSettingSponsor>lambdaQuery()
                .eq(SysApprovalSettingSponsor::getSettingId, setting.getId())
                .eq(SysApprovalSettingSponsor::getIsDeleted, 0));
        Optional.ofNullable(settingSponsors).orElse(new ArrayList<>()).forEach(settingSponsor -> {
            settingSponsor.setId(SnowflakeIdWorker.nextId());
            settingSponsor.setGmtCreate(LocalDateTime.now());
            settingSponsor.setGmtModified(null);
            // 复制生成审批模板附属表发起人范围
            settingSponsorMapper.insert(settingSponsor);
        });
        /**
         * 获取发起人范围 start
         */

        /**
         * 获取步骤数组 start
         */
        List<SysApprovalSettingStep> settingSteps = settingStepMapper.selectList(Wrappers.<SysApprovalSettingStep>lambdaQuery()
                .eq(SysApprovalSettingStep::getSettingId, setting.getId())
                .eq(SysApprovalSettingStep::getIsDeleted, 0));
        Optional.ofNullable(settingSteps).orElse(new ArrayList<>()).forEach(settingStep -> {
            // 获取当前审批步骤的审批条件
            SysApprovalSettingCondition settingCondition = settingConditionMapper.selectOne(Wrappers.<SysApprovalSettingCondition>lambdaQuery()
                    .eq(SysApprovalSettingCondition::getSettingId, setting.getId())
                    .eq(SysApprovalSettingCondition::getStepId, settingStep.getId())
                    .eq(SysApprovalSettingCondition::getIsDeleted, 0));
            if (settingCondition != null) {
                settingCondition.setId(SnowflakeIdWorker.nextId());
                settingCondition.setSettingId(settingId);
                settingCondition.setGmtCreate(LocalDateTime.now());
                settingCondition.setGmtModified(null);
                settingConditionMapper.insert(settingCondition);
            }
            // 根据当前步骤和条件获取审批人
            List<SysApprovalSettingMember> settingMembers = settingMemberMapper.selectList(Wrappers.<SysApprovalSettingMember>lambdaQuery()
                    .eq(SysApprovalSettingMember::getSettingId, setting.getId())
                    .eq(SysApprovalSettingMember::getStepId, settingStep.getId())
                    .eq(SysApprovalSettingMember::getIsDeleted, 0));
            Optional.ofNullable(settingMembers).orElse(new ArrayList<>()).forEach(settingMember -> {
                settingMember.setId(SnowflakeIdWorker.nextId());
                settingMember.setSettingId(settingId);
                settingMember.setGmtCreate(LocalDateTime.now());
                settingMember.setGmtModified(null);
                settingMemberMapper.insert(settingMember);
            });

        });
        /**
         * 获取步骤数组 end
         */

        return settingId;
    }

    /**
     * 删除审批模板相关附属依赖表
     *
     * @param id
     * @param userId
     * @return
     */
    public long delOthers(long id, long userId) {
        /**
         * 删除审批模板副表-发起人范围
         */
        try {
            SysApprovalSettingSponsor settingSponsor = new SysApprovalSettingSponsor();
            settingSponsor.setModifiedId(userId);
            settingSponsor.setIsDeleted(IsDeletedCode.YES.getCode());
            settingSponsorMapper.update(settingSponsor, Wrappers.<SysApprovalSettingSponsor>lambdaQuery().eq(SysApprovalSettingSponsor::getSettingId, id));
        } catch (Exception e) {
            log.error("删除审批模板副表-发起人范围失败了可能没有数据");
        }
        /**
         * 删除审批模板副表-审批步骤
         */
        try {
            SysApprovalSettingStep settingStep = new SysApprovalSettingStep();
            settingStep.setModifiedId(userId);
            settingStep.setIsDeleted(IsDeletedCode.YES.getCode());
            settingStepMapper.update(settingStep, Wrappers.<SysApprovalSettingStep>lambdaQuery().eq(SysApprovalSettingStep::getSettingId, id));
        } catch (Exception e) {
            log.error("删除审批模板副表-审批步骤失败了可能没有数据");
        }
        /**
         * 删除审批模板副表-审批步骤条件
         */
        try {
            SysApprovalSettingCondition settingCondition = new SysApprovalSettingCondition();
            settingCondition.setModifiedId(userId);
            settingCondition.setIsDeleted(IsDeletedCode.YES.getCode());
            settingConditionMapper.update(settingCondition, Wrappers.<SysApprovalSettingCondition>lambdaQuery().eq(SysApprovalSettingCondition::getSettingId, id));
        } catch (Exception e) {
            log.error("删除审批模板副表-审批步骤条件失败了可能没有数据");
        }
        /**
         * 删除审批模板副表-审批步骤审批人员
         */
        try {
            SysApprovalSettingMember settingMember = new SysApprovalSettingMember();
            settingMember.setModifiedId(userId);
            settingMember.setIsDeleted(IsDeletedCode.YES.getCode());
            settingMemberMapper.update(settingMember, Wrappers.<SysApprovalSettingMember>lambdaQuery().eq(SysApprovalSettingMember::getSettingId, id));
        } catch (Exception e) {
            log.error("删除审批模板副表-审批步骤审批人员失败了可能没有数据");
        }
        return id;
    }

    /**
     * 插入审批模板相关附属依赖表
     *
     * @param settingReq
     * @param userId
     * @return
     */
    public long addOthers(ApprovalSettingReq settingReq, long userId) {
        /**
         * 发起人范围 start
         */
        try{
            // 遍历插入部门类型发起人
            Optional.ofNullable(settingReq.getDepts()).orElse(new ArrayList<>()).forEach(department -> {
                SysApprovalSettingSponsor settingSponsor = new SysApprovalSettingSponsor();
                settingSponsor.setId(SnowflakeIdWorker.nextId());
                settingSponsor.setSettingId(settingReq.getId());
                settingSponsor.setSponsorType(1);
                settingSponsor.setSponsorObjectId(department.getId());
                settingSponsor.setSponsorObjectName(department.getTitle());
                settingSponsor.setCreateId(userId);
                try{
                    settingSponsorMapper.insert(settingSponsor);
                }catch (Exception e){
                    log.error(e.getMessage());
                }
            });
        }catch (Exception e){
            log.error("插入部门类型发起人失败！"+e.getMessage());
        }
        try {
            // 遍历插入员工类型发起人
            Optional.ofNullable(settingReq.getEmployees()).orElse(new ArrayList<>()).forEach(employee -> {
                SysApprovalSettingSponsor settingSponsor = new SysApprovalSettingSponsor();
                settingSponsor.setId(SnowflakeIdWorker.nextId());
                settingSponsor.setSettingId(settingReq.getId());
                settingSponsor.setSponsorType(2);
                settingSponsor.setSponsorObjectId(employee.getId());
                settingSponsor.setSponsorObjectName(employee.getTitle());
                settingSponsor.setCreateId(userId);
                try {
                    settingSponsorMapper.insert(settingSponsor);
                } catch (Exception e) {
                    log.error(e.getMessage());
                }
            });
        } catch (Exception e) {
            log.error("插入插入员工类型发起人失败！"+e.getMessage());
        }
        /**
         * 审批步骤配置 start
         */
        if (CollUtil.isNotEmpty(settingReq.getSteps())) {
            for (int i = 0; i < settingReq.getSteps().size(); i++) {
                try{
                    ApprovalSettingStepReq stepReq = settingReq.getSteps().get(i);
                    // 步骤配置
                    SysApprovalSettingStep settingStep = new SysApprovalSettingStep();
                    Long stepId = SnowflakeIdWorker.nextId();
                    settingStep.setId(stepId);
                    settingStep.setSettingId(settingReq.getId());
                    settingStep.setStep(i + 1);
                    settingStep.setStepName(stepReq.getStepName());
                    settingStep.setIsDynamic(stepReq.getIsDynamic());
                    settingStep.setCreateId(userId);
                    int insert = settingStepMapper.insert(settingStep);
                    if(insert > 0){
                        // 步骤条件配置
                        Long conditionId = null;
                        if (null != stepReq.getCondition()) {
                            try{
                                SysApprovalSettingCondition settingCondition = new SysApprovalSettingCondition();
                                conditionId = SnowflakeIdWorker.nextId();
                                settingCondition.setId(conditionId);
                                settingCondition.setSettingId(settingReq.getId());
                                settingCondition.setStepId(stepId);
                                settingCondition.setValueBegin(null != stepReq.getCondition() ? stepReq.getCondition().getValueBegin() : BigDecimal.valueOf(0));
                                settingCondition.setValueEnd(null != stepReq.getCondition() ? stepReq.getCondition().getValueEnd() : BigDecimal.valueOf(0));
                                settingCondition.setCreateId(userId);
                                settingConditionMapper.insert(settingCondition);
                            }catch (Exception e){
                                log.error(e.getMessage());
                            }
                        }
                        // 步骤主审批人配置
                        if (null != stepReq.getMasterMember()) {
                            try {
                                try {
                                    Long finalConditionId = conditionId;
                                    Optional.ofNullable(stepReq.getMasterMember().getDepts()).orElse(new ArrayList<>()).forEach(dept -> {
                                        SysApprovalSettingMember master = new SysApprovalSettingMember();
                                        master.setId(SnowflakeIdWorker.nextId());
                                        master.setSettingId(settingReq.getId());
                                        master.setStepId(stepId);
                                        master.setIsRelationCondition(1);
                                        master.setConditionId(finalConditionId);
                                        master.setApproverType(1);
                                        master.setApproverId(dept.getId());
                                        master.setApproverName(dept.getTitle());
                                        master.setCreateId(userId);
                                        settingMemberMapper.insert(master);
                                    });
                                }catch (Exception e){
                                    log.error(e.getMessage());
                                }
                                try {
                                    Long finalConditionId1 = conditionId;
                                    Optional.ofNullable(stepReq.getMasterMember().getEmployees()).orElse(new ArrayList<>()).forEach(employee -> {
                                        SysApprovalSettingMember master = new SysApprovalSettingMember();
                                        master.setId(SnowflakeIdWorker.nextId());
                                        master.setSettingId(settingReq.getId());
                                        master.setStepId(stepId);
                                        master.setIsRelationCondition(1);
                                        master.setConditionId(finalConditionId1);
                                        master.setApproverType(2);
                                        master.setApproverId(employee.getId());
                                        master.setApproverName(employee.getTitle());
                                        master.setCreateId(userId);
                                        settingMemberMapper.insert(master);
                                    });
                                } catch (Exception e) {
                                    log.error(e.getMessage());
                                }
                            } catch (Exception e) {
                                log.error(e.getMessage());
                            }
                        }
                        if (null != stepReq.getSlaveMember()) {
                            try {

                                // 步骤副审批人配置
                                Optional.ofNullable(stepReq.getSlaveMember().getDepts()).orElse(new ArrayList<>()).forEach(dept -> {
                                    SysApprovalSettingMember slave = new SysApprovalSettingMember();
                                    slave.setId(SnowflakeIdWorker.nextId());
                                    slave.setSettingId(settingReq.getId());
                                    slave.setStepId(stepId);
                                    slave.setIsRelationCondition(0);
                                    slave.setConditionId(null);
                                    slave.setApproverType(1);
                                    slave.setApproverId(dept.getId());
                                    slave.setApproverName(dept.getTitle());
                                    slave.setCreateId(userId);
                                    settingMemberMapper.insert(slave);
                                });
                            } catch (Exception e) {
                                log.error(e.getMessage());
                            }
                            try {
                                Optional.ofNullable(stepReq.getSlaveMember().getEmployees()).orElse(new ArrayList<>()).forEach(employee -> {
                                    SysApprovalSettingMember slave = new SysApprovalSettingMember();
                                    slave.setId(SnowflakeIdWorker.nextId());
                                    slave.setSettingId(settingReq.getId());
                                    slave.setStepId(stepId);
                                    slave.setIsRelationCondition(0);
                                    slave.setConditionId(null);
                                    slave.setApproverType(2);
                                    slave.setApproverId(employee.getId());
                                    slave.setApproverName(employee.getTitle());
                                    slave.setCreateId(userId);
                                    settingMemberMapper.insert(slave);
                                });
                            } catch (Exception e) {
                                log.error(e.getMessage());
                            }
                        }
                    }
                }catch (Exception e){
                    log.error("审批步骤配置错误了一条数据："+e.getMessage());
                }
            }
        }
        return settingReq.getId();
    }

}
